package com.molichuxing.services.business.service.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.business.convertor.Convert;
import com.molichuxing.services.business.dto.response.ComplaintBizDto;
import com.molichuxing.services.business.service.ComplaintBizService;
import com.molichuxing.services.infrastructure.dto.response.ComplaintDto;
import com.molichuxing.services.infrastructure.dto.response.UserDto;
import com.molichuxing.services.infrastructure.service.ComplaintService;
import com.molichuxing.services.infrastructure.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author liyg
 * 用户投诉组合服务实现类
 * 2019-08-14
 */
@Service("complaintBizService")
public class ComplaintBizServiceImpl implements ComplaintBizService {

  @Autowired
  private ComplaintService complaintService;

  @Autowired
  private UserService userService;

  /**
	 * 获取投诉列表
	 * 
	 * @param map
	 *            参数
	 *key:phone
	 *            String 用户手机号 非必填
	 *key:realName
	 *            String 用户真实姓名 非必填
	 *key:type
	 *            Integer 投诉方式 非必填
	 *key:status
	 *            Integer 投诉处理状态 非必填
	 *key:startTime
	 *            String 投诉开始时间 非必填
	 *key:endTime
	 *            String 投诉结束时间 非必填
	 *key:pageNum
	 *            Integer 当前页
	 *key:pageSize
	 *            Integer 每页显示条数
	 * @return
	 */
  @Override
  public Paged<ComplaintBizDto> getComplaintList(Map<String, Object> mapParam) {

    String phone = ObjectCastUtil.castString(mapParam.get("phone"));
    String realName = ObjectCastUtil.castString(mapParam.get("realName"));

    UserDto userBeanDto = new UserDto();
    if (StringUtils.isNoneBlank(phone) || StringUtils.isNoneBlank(realName)) {
      userBeanDto = userService.getUser(null,realName,phone);
    }
    if (null == userBeanDto) {
      return null;
    }

    /*获取投诉建议列表*/
    Integer id = userBeanDto.getId();
    mapParam.put("userId", id);
    Paged<ComplaintDto> paged = complaintService.getComplaintList(mapParam);
    if(null == paged) {
    	return null;
    }else if(paged.isEmpty()) {
    	return  new Paged<>(null,paged.getTotal(),paged.getPageSize());
    }
    
    List<ComplaintBizDto> complaintBizDtoList = Convert.toComplaintBizDto(paged.getList());

    /*查询用户信息*/
    List<Integer> ids = new ArrayList<>();
    for (ComplaintBizDto complaintBizDto : complaintBizDtoList) {
      ids.add(complaintBizDto.getUserId());
    }
    
    /*查询用户信息*/
    Map<Integer, UserDto> map = userService.getUsers(ids);

    /*封装用户参数*/
    if(null != map) {
    	for (ComplaintBizDto dto : complaintBizDtoList) {
    	  UserDto  userDto = map.get(dto.getUserId());
  	      dto.setRealName(userDto == null ? null : userDto.getRealName());
  	      dto.setPhone(userDto == null ? null :userDto.getPhone());
    	}
    }

    Long total = paged.getTotal();
    Integer pages = paged.getPages();
    Paged<ComplaintBizDto> page = new Paged<>(complaintBizDtoList,total,pages);

    return page;
  }

}
